package com.ficus.road.maintain.handler.smartdecision.strategy;

import com.ficus.road.maintain.core.configs.SmartDecisionConfig;
import com.ficus.road.maintain.core.model.smartnew.SmartDecisionExcelVO;
import com.ficus.road.maintain.core.model.smartnew.SmartDecisionResponse;
import com.ficus.road.maintain.core.model.smartnew.SmartTable;
import com.ficus.road.maintain.handler.smartdecision.enums.SmartDecisionStrategyEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/7/20 下午8:36
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class SmartDecisionHighwaySortingStrategy implements ISmartDecisionStrategy {
    private static final String IS_FINAL_RESULT = "是";
    private final SmartDecisionConfig smartDecisionConfig;

    @Override
    public SmartDecisionStrategyEnum getStrategy() {
        return SmartDecisionStrategyEnum.HIGHWAY_SORTING;
    }

    @Override
    public SmartDecisionResponse process(List<SmartDecisionExcelVO> params, SmartDecisionResponse response) {
        List<SmartTable> smartTables = new ArrayList<>();
        List<SmartTable> reserves = new ArrayList<>();
        int overhaulYearConfig = smartDecisionConfig.getHighwayConfig().getOverhaulYear();
        int newBuildYearConfig = smartDecisionConfig.getHighwayConfig().getNewBuildYear();
        params.forEach(param -> {
            // 5个影响对象创建的参数
            String source = param.getSource();
            String character = param.getRoadCharacter();
            String level = param.getLevel();
            Double pci = param.getPCI();
            Double rqi = param.getRQI();
            SmartTable smartTable = null;
            SmartTable reserve = null;
            Double lastOverhaulYear = param.getLastOverhaulYear();
            Double newBuildYear = param.getNewBuildYear();
            // todo: 这里可以用工厂模式优化一下？
            if (ObjectUtils.isNotEmpty(lastOverhaulYear) && ObjectUtils.isNotEmpty(newBuildYear) && (lastOverhaulYear >= overhaulYearConfig || newBuildYear >= newBuildYearConfig)) {
                reserve = new SmartTable(param, "储备库");
            } else {
                if ("重点区域".equals(source)) {
                    smartTable = new SmartTable(param, "第一大类");
                } else if ("街镇需求".equals(source)) {
                    smartTable = new SmartTable(param, "第二大类");
                } else if ("路况需求".equals(source)) {
                    if (ObjectUtils.isNotEmpty(character) && character.contains("沥青")) {
                        if (level.contains("高速") || level.contains("一级")) {
                            if (pci >= 80 && rqi >= 80) {
                                reserve = new SmartTable(param, "储备库");
                            } else if (pci >= 80 && rqi < 80) {
                                smartTable = new SmartTable(param, "第四大类");
                            } else if (pci < 80 && rqi >= 80) {
                                smartTable = new SmartTable(param, "第五大类");
                            } else {
                                smartTable = new SmartTable(param, "第三大类");
                            }
                        } else if (level.contains("二级干")) {
                            if (pci >= 75 && rqi >= 80) {
                                reserve = new SmartTable(param, "储备库");
                            } else if (pci >= 75 && rqi < 80) {
                                smartTable = new SmartTable(param, "第四大类");
                            } else if (pci < 75 && rqi >= 80) {
                                smartTable = new SmartTable(param, "第五大类");
                            } else {
                                smartTable = new SmartTable(param, "第三大类");
                            }
                        } else if (level.contains("二级非")) {
                            if (pci >= 75 && rqi >= 75) {
                                reserve = new SmartTable(param, "储备库");
                            } else if (pci >= 75 && rqi < 75) {
                                smartTable = new SmartTable(param, "第四大类");
                            } else if (pci < 75 && rqi >= 75) {
                                smartTable = new SmartTable(param, "第五大类");
                            } else {
                                smartTable = new SmartTable(param, "第三大类");
                            }
                        } else {
                            if (pci >= 70 && rqi >= 70) {
                                reserve = new SmartTable(param, "储备库");
                            } else if (pci >= 70 && rqi < 70) {
                                smartTable = new SmartTable(param, "第四大类");
                            } else if (pci < 70 && rqi >= 70) {
                                smartTable = new SmartTable(param, "第五大类");
                            } else {
                                smartTable = new SmartTable(param, "第三大类");
                            }
                        }
                    } else {
                        smartTable = new SmartTable(param, "第三大类");
                    }
                } else {
                    reserve = new SmartTable(param, "储备库");
                }
            }
            if (ObjectUtils.isNotEmpty(smartTable)) {
                smartTables.add(smartTable);
            }
            if (ObjectUtils.isNotEmpty(reserve)) {
                reserves.add(reserve);
            }
        });
        response.getSmartSortingResponse().setTablesBeforeMerge(smartTables);
        response.getSmartSortingResponse().setReservesBeforeMerged(reserves);
        return response;
    }
}
